using System.Diagnostics;

namespace DmServer.Handlers.Handlers;

public class BindWindowEx : IMessageHandler
{
    public void HandleMessage(StringRequestInfo? request, TcpSessionClient client, DmSoft dm)
    {
        if (request?.Parameters == null || request.Parameters.Length != 6)
            client.SendAsync(CryptoHelper.EncryptDES("协议命令错误！"));

        int.TryParse(request?.Parameters?[0], out int hwnd);
        string display = request?.Parameters?[1] ?? "";
        string mouse = request?.Parameters?[2] ?? "";
        string keypad = request?.Parameters?[3] ?? "";
        string public_desc = request?.Parameters?[4] ?? "";
        int.TryParse(request?.Parameters?[5], out int mode);
        Stopwatch stopwatch = new Stopwatch();
        // 启动计时
        stopwatch.Start();
        var result = dm.BindWindowEx(hwnd, display, mouse, keypad, public_desc, mode);
        client.Logger.Info("BindWindowEx：" + result.ToString());
        // 停止计时
        stopwatch.Stop();
        client.Logger.Info($"代码段耗时: {stopwatch.ElapsedMilliseconds} 毫秒");
        // 回复客户端消息
        client.SendAsync(CryptoHelper.EncryptDES(result.ToString()));
    }
}
